<!--Global Site Tag (gtag.js) - Google Analytics -->
{%- assign adwords = storeConfig.google_adwords_trace_id | strtoarray -%}
{%- assign tracking = storeConfig.google_analytics_trace_id | strtoarray -%}
{%- assign conversion = storeConfig.google_conversion_id | strtoarray -%}
{%- assign sales_open = storeConfig.sales_use_goods_amount -%}
{%- if tracking.0 -%}
    {%- assign default_tracking_id = tracking.0 -%}
{%- elsif adwords.0 -%}
    {%- assign default_tracking_id = adwords.0 -%}
{%- endif -%}
<script async src="https://www.googletagmanager.com/gtag/js?id={{ default_tracking_id }}"></script>
<script>
    window.dataLayer = window.dataLayer || [];

    function gtag() {
        dataLayer.push(arguments);
    }

    gtag('js', new Date());

    {%- if tracking -%}
    {% for pixel in tracking %}
    gtag('config', '{{ pixel }}');
    {%- endfor -%}
    {%- endif -%}
	{% if sales_open %}
    gtag('set', {'currency': '{{ base_currency.standard_code }}'});
    gtag('require', 'ec');
    {%- endif -%}

    {%- if adwords -%}
    {% for pixel in adwords %}
    gtag('config', '{{ pixel }}');
    {%- endfor -%}
    {%- endif -%}
    
    {%- if template == 'product_detail' -%}

    {% if sales_open and tracking %}

    gtag('event', 'view_item', {
        'send_to': [{{ tracking | strquotes }}],
        'items': [{
            "id": "{{ product.spu }}",
            "category":"{{ product.product_type | escape}}",
            "variant": "{{ product.variant.sku_value| replace: "<br />", ", " | escape | strip_newlines }}",
            "list_position": 1,
            "quantity": 1,
            "price": "{{ product.variant.price }}"
        }],
        "value":"{{product.variant.price}}",
        "currency":"{{currency.standard_code}}"
    });
    {%- endif -%}

    {% if adwords %}

    gtag('event', 'view_item', {
        'send_to': [{{ adwords | strquotes }}],
        'ecomm_pagetype': 'product',
        'ecomm_pcat': ['{{ product.product_type | escape | append: ' '}}'.replace(/\s+$/g, "")],
        'ecomm_prodid': ["{{ product.spu }}"],
        'ecomm_pvalue': '{{ product.variant.price }}',
        'ecomm_totalvalue': '{{ product.variant.price }}',
    });
    {%- endif -%}

    {% elsif template == 'search_detail' and search.keywords %}
        {%- if tracking -%}
        gtag('event', 'view_search_results', {
        'send_to': [{{ tracking | strquotes }}],
        'search_term': '{{ search.keywords | escape | append: ' ' }}'.replace(/\s+$/g, "")
        });

        {%- endif -%}

    {% if adwords %}
    var pcat =[];
    var prodid =[];
    var pname =[];
    {%- if search -%}
    {%- for product in search.products -%}
    pcat.push('{{ product.product_type | escape | append: ' '}}'.replace(/\s+$/g, ""));
    prodid.push("{{ product.spu | escape}}");
    {%- endfor -%}
    {%- endif -%}
    gtag('event', 'search', {
    'send_to': [{{ adwords | strquotes }}], 'view_search_results': '{{ search.keywords | escape  | append: ' '}}'.replace(/\s+$/g, ""),
    'ecomm_pagetype': 'searchresults',
    'ecomm_pcat': pcat,
    'ecomm_prodid': prodid,
    });
    {%- endif -%}

    {% elsif template == 'collection_detail' %}
    
	gtag('event', 'page_view_event', {
		'send_to': [{{ tracking | strquotes }}], 
		'event_category':'{{ template }}',
		'event_label':'{{collection.title| escape}}',
		'value':'{{collection.id}}'
	});

	{%- if sales_open and tracking -%}
    gtag('event', 'view_item_list', {
    'send_to': [{{ tracking | strquotes }}],
    'items': [
    {%- if collection.products -%}
    {%- for product in collection.products -%}
    {
    "id": "{{ product.spu }}",
    "list_name":'{{ collection.title | escape}}',
    "category": '{{ product.product_type | escape | append: ' '}}'.replace(/\s+$/g, ""),
    "variant": "{{ product.variant.sku_value | replace: "<br />", ", " | escape | strip_newlines }}",
    "list_position": {{ forloop.index }},
    "quantity": 1,
    "price": "{{ product.variant.price }}"
    },
    {%- endfor -%}
    {%- endif -%}
    ]
    });

    {%- endif -%}

    {%- if adwords -%}
    var pcat =[];
    var prodid =[];
    var pname =[];
    {%- if collection.products -%}
    {%- for product in collection.products -%}
    pcat.push("{{ product.product_type | escape}}");
    prodid.push("{{ product.spu | escape}}");
    {%- endfor -%}
    {%- endif -%}
    gtag('event', 'view_item_list', {
        'send_to': [{{ adwords | strquotes }}],
        'ecomm_pagetype': 'category',
        'ecomm_pcat': pcat,
        'ecomm_prodid': prodid,
    });
    {%- endif -%}

    {%- elsif controller == 'order' and action == 'success' -%}

    {%- if order -%}
    {%- if sales_open and tracking -%}
    gtag('event', 'purchase', {
        'send_to': [{{ tracking | strquotes }}],
        'transaction_id': '{{ order.order_number }}',
        {%- if storeConfig.google_enable_ga -%}
        'value': "{{ order.current_subtotal_price| plus:order.current_coupon_price | plus:order.current_promotion_price }}",
        {%- else -%}
        'value': '{{ order.total_price }}',
        {%- endif -%}
        'currency': '{{ order.currency_code }}',
        'tax': '{{ order.current_tax_price }}',
        'shipping': '{{ order.current_shipping_price }}',
        'coupon': '{{ order.current_coupon_price }}',
        'items': [
            {%- if order.products -%}
            {%- for product in order.products -%}
            {
                "id": "{{ product.spu | escape}}",
                "category": '{{ product.product_type | escape}}',
                "variant": "{{ product.sku_value| replace: "<br />", ", " | strip_newlines }}",
                "list_position": {{ forloop.index }},
                "quantity": "{{ product.quantity }}",
                {%- if storeConfig.google_enable_ga -%}
                "price": "{{ product.discount_price }}"
                {%- else -%}
                 "price": "{{ product.price }}"
                {%- endif -%}

            },
            {%- endfor -%}
            {%- endif -%}
        ]
    });
    {%- endif -%}

    {%- if adwords -%}
    var pcat =[];
    var prodid =[];
    var pname =[];
    {%- if order.products -%}
    {%- for product in order.products -%}
    pcat.push("{{ product.product_type | escape}}");
    prodid.push("{{ product.spu | escape}}");
    {%- endfor -%}
    {%- endif -%}
    gtag('event', 'purchase', {
		'send_to': [{{ adwords | strquotes }}],
		'ecomm_pagetype': 'purchase',
		'ecomm_pcat': pcat,
		'ecomm_prodid': prodid,
		{%- if storeConfig.google_enable_ga -%}
		'ecomm_pvalue': '{{ order.current_subtotal_price | plus:order.current_coupon_price | plus:order.current_promotion_price}}',
		'ecomm_totalvalue': '{{ order.current_subtotal_price | plus:order.current_coupon_price | plus:order.current_promotion_price}}',
		{%- else -%}
		'ecomm_pvalue': '{{ order.total_price }}',
		'ecomm_totalvalue': '{{ order.total_price }}'
		{%- endif -%}
    });
    {%- endif -%}

    {%- if conversion -%}
    gtag('event', 'conversion', {
        'send_to': [{{ conversion | strquotes }}],
        {% if storeConfig.google_enable_ga %}
        'value': "{{ order.current_subtotal_price | plus:order.current_coupon_price | plus:order.current_promotion_price}}",
        {% else %}
        'value': '{{ order.total_price }}',
        {% endif %}
        "currency": "{{ order.currency_code }}",
        "transaction_id": "{{ order.order_number }}"
    });
    {%- endif -%}
    {%- endif -%}


    {%- elsif controller == 'index' and action == 'index' -%}
    {%- if tracking -%}
    gtag('event', 'index', {
		'send_to': [{{ tracking | strquotes }}]
	});
    {%- endif -%}
    {%- if adwords -%}
    gtag('event', 'index', {
		'send_to': [{{ adwords | strquotes }}],
		'ecomm_pagetype': 'home'
	});
    {%- endif -%}

    {%- elsif template == 'blog_detail' -%}
    {%- if tracking -%}
    gtag('event', 'page_view_event', {
		'send_to': [{{ tracking | strquotes }}], 
		'event_category':'{{ template }}',
		'event_label':'{{blog.title| escape}}',
		'value':'{{blog.id}}'
	});
    {%- endif -%}
    {%- if adwords -%}
    gtag('event', '{{template}}', {
		'send_to': [{{ adwords | strquotes }}]
	});
    {%- endif -%}

    {%- elsif template == 'page_detail' -%}
    {%- if tracking -%}
    gtag('event', 'page_view_event', {
		'send_to': [{{ tracking | strquotes }}], 
		'event_category':'{{ template }}',
		'event_label':'{{page.title| escape}}',
		'value':'{{page.id}}'
	});
    {%- endif -%}
    {%- if adwords -%}
    gtag('event', '{{template}}', {
		'send_to': [{{ adwords | strquotes }}]
	});
    {%- endif -%}

    {%- else -%}
    {%- if tracking -%}
    gtag('event', 'page_view_event', {
		'send_to': [{{ tracking | strquotes }}],
		'event_category':'{{ template }}'
	});
    {%- endif -%}
    {%- if adwords -%}
    gtag('event', '{{template}}', {'send_to': [{{ adwords | strquotes }}]});
    {%- endif -%}


    {% endif %}

    function callback_google_add_to_cart(ecdata) {
        {% if tracking and sales_open %}
        gtag('event', 'add_to_cart', {
			'send_to': [{{ tracking | strquotes }}], 
			'items': [{
				"id": ecdata['ec_data_spu'],
                "category": ecdata['ec_data_category'],
                "variant": ecdata['ec_data_sku_name'],
                "list_position": 1,
                "quantity": ecdata['ec_data_quantity'],
                "price":  formatMoney(ecdata['ec_data_price'],oemcart_base_currency,'',false),
            }],
            "value":  formatMoney( ecdata['ec_data_price'] *  ecdata['ec_data_quantity'],oemcart_base_currency,'',false),
            "currency":"{{currency.standard_code}}"
        });
        {%- endif -%}
        {% if adwords %}
        var pcat =[];
        var prodid =[];
        var pname =[];
        pcat.push(ecdata['ec_data_category']);
        prodid.push(ecdata['ec_data_spu']);
        pname.push(ecdata['ec_data_name']);
        gtag('event', 'add_to_cart', {
            'send_to': [{{ adwords | strquotes }}],
            'ecomm_pagetype': 'cart',
            'ecomm_pcat': pcat,
            'ecomm_prodid': prodid,
            'ecomm_pvalue': formatMoney(ecdata['ec_data_price'],oemcart_base_currency,'',false),
            'ecomm_totalvalue': ecdata['ec_data_price'] * ecdata['ec_data_quantity']
        });
        {%- endif -%}

    }


    function callback_google_generate_lead() {
        {%- if tracking -%}
        gtag('event', 'generate_lead', {
			'send_to': [{{ tracking | strquotes }}]
		});
        {%- endif -%}
        {%- if adwords -%}
        gtag('event', 'generate_lead', {
			'send_to': [{{ adwords | strquotes }}]
		});
        {%- endif -%}
    }

    function callback_google_sign_up() {
        {%- if tracking -%}
        gtag('event', 'sign_up', {
			'send_to': [{{ tracking | strquotes }}]
		});
        {%- endif -%}
        {%- if adwords -%}
        gtag('event', 'sign_up', {
			'send_to': [{{ adwords | strquotes }}]
		});
        {%- endif -%}
    }

    function callback_google_login() {
        {%- if tracking -%}
        gtag('event', 'login', {'send_to': [{{ tracking | strquotes }}]});
        {%- endif -%}
        {%- if adwords -%}
        gtag('event', 'login', {'send_to': [{{ adwords | strquotes }}]});
        {%- endif -%}
    }


    function callback_google_begin_checkout() {
        {%- if sales_open and tracking -%}
        gtag('event', 'begin_checkout', {
			'send_to': [{{ tracking | strquotes }}]
		});
        {%- endif -%}
        {%- if adwords -%}
        gtag('event', 'begin_checkout', {
			'send_to': [{{ adwords | strquotes }}]
		});
        {%- endif -%}
    }

    function callback_google_share_product(ec_data, platform) {
        {%- if tracking -%}
        gtag('event', 'share_product', {
			'send_to': [{{ tracking | strquotes }}], 
			'event_category':ec_data['ec_data_category'],
			'event_label':ec_data['ec_data_spu'],
			"value":  formatMoney(ec_data['ec_data_price'],oemcart_base_currency,'',false),
            "currency":"{{currency.standard_code}}"
		});
        {%- endif -%}
        {%- if adwords -%}
        gtag('event', 'share_product', {'send_to': [{{ adwords | strquotes }}]});
        {%- endif -%}
    }

    function callback_google_add_address_information() {
        {%- if tracking -%}
        gtag('event', 'checkout_progress', {
        	'send_to': [{{ tracking | strquotes }}],
        	'checkout_step': 1
        });
        gtag('event', 'add_address_information', {
        	'send_to': [{{ tracking | strquotes }}]
        });
        {%- endif -%}
        {%- if adwords -%}
        gtag('event', 'add_address_information', {
			'send_to': [{{ adwords | strquotes }}]
		});
        {%- endif -%}
    }

    function callback_google_add_shipping_information() {
        {%- if tracking -%}
        gtag('event', 'checkout_progress', {
        	'send_to': [{{ tracking | strquotes }}],
        	'checkout_step': 2
        });
        gtag('event', 'add_shipping_information', {
        	'send_to': [{{ tracking | strquotes }}]
        });
        {%- endif -%}
        {%- if adwords -%}
        gtag('event', 'add_shipping_information', {
			'send_to': [{{ adwords | strquotes }}]
		});
        {%- endif -%}
    }

    function callback_google_add_payment_information() {
            {%- if tracking -%}
            gtag('event', 'checkout_progress', {
            	'send_to': [{{ tracking | strquotes }}],
            	'checkout_step': 3
            });
            gtag('event', 'add_payment_information', {
        	'send_to': [{{ tracking | strquotes }}]
        });
            {%- endif -%}
            {%- if adwords -%}
            gtag('event', 'add_payment_information', {
				'send_to': [{{ adwords | strquotes }}]
			});
            {%- endif -%}
    }

    function callback_google_remove_from_cart(ec_data) {
        {%- if tracking -%}
        gtag('event', 'remove_from_cart', {
			'send_to': [{{ tracking | strquotes }}], 
			'items': [{
				"id": ec_data['ec_data_spu'],
				"category": ec_data['ec_data_category'],
				"variant": ec_data['ec_data_sku_name'].replace('<br />',', '),
				"list_position": 1,
				"quantity": ec_data['ec_data_quantity'],
				"price": formatMoney(ec_data['ec_data_price'],oemcart_base_currency,'',false),
        	}],
            "value": formatMoney(ec_data['ec_data_price'] *  ec_data['ec_data_quantity'],oemcart_base_currency,'',false),
            "currency":"{{currency.standard_code}}"
        });
        {%- endif -%}
        {%- if adwords -%}
        gtag('event', 'remove_from_cart', {
			'send_to': [{{ adwords | strquotes }}]
		});
        {%- endif -%}
    }

    function callback_google_to_wishlist(){}

</script>
<!-- End Global Site Tag (gtag.js) - Google Analytics -->